package com.sinoglobal.sinostore.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.lidroid.xutils.BitmapUtils;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.SinoStore;
import com.sinoglobal.sinostore.bean.ImageVo;
import com.sinoglobal.sinostore.system.Constants;


public class BannerApater extends BaseAdapter {
	private Context context;
	private ArrayList<ImageVo> images;
	private BitmapUtils bitmapUtils;

	public BannerApater(Context context, ArrayList<ImageVo> arrayList) {
		this.context = context;
		this.images = arrayList;
		 bitmapUtils = new BitmapUtils(context);
		 bitmapUtils.configDefaultLoadingImage(R.drawable.shop_defaultimghome_content_img_middle);
		 bitmapUtils.configDefaultLoadFailedImage(R.drawable.shop_defaultimghome_content_img_middle);
		 bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);
	}

	public int getCount() {
		if (images.size() == 1) {
			return 1;
		}
		return Integer.MAX_VALUE;
	}

	public Object getItem(int position) {

		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings("deprecation")
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			ImageView imageView = new ImageView(context);
			imageView.setAdjustViewBounds(true);
			imageView.setScaleType(ScaleType.CENTER_CROP);
			imageView.setLayoutParams(new Gallery.LayoutParams(SinoStore.WIDTHPIXELS, SinoStore.WIDTHPIXELS * 280 / 480));
			convertView = imageView;
			viewHolder.imageView = (ImageView) convertView;
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		bitmapUtils.display(viewHolder.imageView, Constants.PIC_HOST + images.get(position % images.size()).getImg_url());
		return convertView;
	}
	private class ViewHolder{
		ImageView imageView;
	}
}

